{include file="public/header" /}
<body>
<div class="layui-fluid">
    <div class="layui-card" >
        <div class="layui-tab" lay-filter="settab">
            <ul class="layui-tab-title">
                <li class="layui-this">基本设置</li>
                <li>微信设置</li>
            </ul>
            <div class="layui-tab-content">
                <div class="layui-tab-item layui-show">
                    <form class="layui-form" action="" id="baseset" lay-filter="baseset">
                        <div class="layui-form-item">
                            <div class="layui-inline">
                                <label class="layui-form-label">系统名称</label>
                                <div class="layui-input-inline">
                                    <input type="text" name="title" lay-verify="required|title" autocomplete="off" class="layui-input">
                                </div>
                                <div class="layui-form-mid layui-word-aux">请填系统名称</div>
                            </div>
                        </div>

                        <div class="layui-form-item">
                            <div class="layui-inline">
                                <label class="layui-form-label">LOGO</label>
                                <div class="layui-input-inline">
                                    <div class="layui-upload-drag" id="upload1">
                                        <i class="layui-icon"></i>
                                        <p>点击上传，或将文件拖拽到此处</p>
                                        <div class="layui-hide" id="uploadLogo">
                                            <hr>
                                            <img src="" alt="上传成功后渲染" style="max-width: 196px">
                                        </div>
                                    </div>
                                </div>
                                <div class="layui-form-mid layui-word-aux">请填上传LOGO</div>
                            </div>
                        </div>

                        <div class="layui-form-item">
                            <div class="layui-inline">
                                <label class="layui-form-label">版权信息</label>
                                <div class="layui-input-inline">
                                    <input type="text" name="copyright" lay-verify="copyright" autocomplete="off" class="layui-input">
                                </div>
                                <div class="layui-form-mid layui-word-aux">请填写版权信息</div>
                            </div>
                        </div>

                        <div class="layui-form-item">
                            <div class="layui-inline">
                                <label class="layui-form-label">备案信息</label>
                                <div class="layui-input-inline">
                                    <input type="text" name="copyrighturl" lay-verify="copyrighturl" autocomplete="off" class="layui-input">
                                </div>
                                <div class="layui-form-mid layui-word-aux">请填写备案信息</div>
                            </div>
                        </div>

                        <div class="layui-form-item">
                            <div class="layui-input-block">
                                <button type="submit" class="layui-btn" lay-submit="" lay-filter="savedata1">保存设置</button>
                            </div>
                        </div>
                    </form>
                </div>
                <div class="layui-tab-item">

                    <form class="layui-form" action="" id="wxset" lay-filter="wxset">
                        <div class="layui-form-item">
                            <div class="layui-inline">
                                <label class="layui-form-label">随机SALT</label>
                                <div class="layui-input-inline">
                                    <input type="text" name="salt" lay-verify="salt" autocomplete="off" class="layui-input">
                                </div>
                                <div class="layui-form-mid layui-word-aux"></div>
                            </div>

                            <div class="layui-inline">
                                <label class="layui-form-label">微信用户表名</label>
                                <div class="layui-input-inline">
                                    <input type="text" name="tablename" lay-verify="tablename" autocomplete="off" class="layui-input">
                                </div>
                                <div class="layui-form-mid layui-word-aux"></div>
                            </div>

                        </div>

                        <div class="layui-form-item">
                            <div class="layui-inline">
                                <label class="layui-form-label">跳转域名</label>
                                <div class="layui-input-inline">
                                    <input type="text" name="domain" lay-verify="domain" autocomplete="off" class="layui-input">
                                </div>
                                <div class="layui-form-mid layui-word-aux">如http://www.baidu.com或为空（公众号需授权域名）</div>
                            </div>
                        </div>


                        <div class="layui-form-item">
                            <div class="layui-inline">
                                <label class="layui-form-label">微信AppID</label>
                                <div class="layui-input-inline">
                                    <input type="text" name="appid" lay-verify="appid" autocomplete="off" class="layui-input">
                                </div>
                                <div class="layui-form-mid layui-word-aux"></div>
                            </div>
                        </div>

                        <div class="layui-form-item">
                            <div class="layui-inline">
                                <label class="layui-form-label">微信AppSecret</label>
                                <div class="layui-input-inline">
                                    <input type="text" name="appsecret" lay-verify="appsecret" autocomplete="off" class="layui-input">
                                </div>
                                <div class="layui-form-mid layui-word-aux">公众号需设置IP白名单</div>
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <div class="layui-inline">
                                <label class="layui-form-label">mch_id</label>
                                <div class="layui-input-inline">
                                    <input type="text" name="mch_id" lay-verify="mch_id" autocomplete="off" class="layui-input">
                                </div>
                                <div class="layui-form-mid layui-word-aux">mch_id</div>
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <div class="layui-inline">
                                <label class="layui-form-label">key</label>
                                <div class="layui-input-inline">
                                    <input type="text" name="key" lay-verify="key" autocomplete="off" class="layui-input">
                                </div>
                                <div class="layui-form-mid layui-word-aux">key</div>
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <div class="layui-inline">
                                <label class="layui-form-label">支付回调地址</label>
                                <div class="layui-input-inline">
                                    <input type="text" name="notifyurl" lay-verify="notifyurl" autocomplete="off" class="layui-input">
                                </div>
                                <div class="layui-form-mid layui-word-aux">notifyurl</div>
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <div class="layui-inline">
                                <label class="layui-form-label">登录回调地址</label>
                                <div class="layui-input-inline">
                                    <input type="text" name="returnurl" lay-verify="returnurl" autocomplete="off" class="layui-input">
                                </div>
                                <div class="layui-form-mid layui-word-aux">returnurl</div>
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <div class="layui-inline">
                                <label class="layui-form-label">上传核验文件</label>
                                <div class="layui-input-inline">
                                    <div class="layui-upload-drag" id="upload2">
                                        <i class="layui-icon"></i>
                                        <p>点击上传，或将文件拖拽到此处</p>
                                        <div class="layui-hide" id="mp_verify">
                                            <hr>
                                            <img src="" alt="上传成功后渲染" style="max-width: 196px">
                                        </div>
                                    </div>
                                </div>
                                <input type="hidden" name="mp_verify" value="">
                                <div class="layui-form-mid layui-word-aux"></div>
                            </div>

                        </div>

                        <div class="layui-form-item">
                            <div class="layui-inline">
                                <label class="layui-form-label">商户key</label>
                                <div class="layui-input-inline">
                                    <div class="layui-upload-drag" id="upload3">
                                        <i class="layui-icon"></i>
                                        <p>点击上传，或将文件拖拽到此处</p>
                                        <div class="layui-hide" id="mch_key">
                                            <hr>
                                            <img src="" alt="上传成功后渲染" style="max-width: 196px">
                                        </div>
                                    </div>
                                </div>
                                <input type="hidden" name="mch_key" value="">
                                <div class="layui-form-mid layui-word-aux"></div>
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <div class="layui-inline">
                                <label class="layui-form-label">商户cert</label>
                                <div class="layui-input-inline">
                                    <div class="layui-upload-drag" id="upload4">
                                        <i class="layui-icon"></i>
                                        <p>点击上传，或将文件拖拽到此处</p>
                                        <div class="layui-hide" id="mch_cert">
                                            <hr>
                                            <img src="" alt="上传成功后渲染" style="max-width: 196px">
                                        </div>
                                    </div>
                                </div>
                                <input type="hidden" name="mch_cert" value="">
                                <div class="layui-form-mid layui-word-aux"></div>
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <div class="layui-input-block">
                                <button type="submit" class="layui-btn" lay-submit="" lay-filter="savedata2">保存设置</button>
                            </div>
                        </div>
                    </form>

                </div>
            </div>
        </div>

    </div>

</div>

<style>
    .layui-form-label{
        width: 100px;
    }
    .layui-input-block {
        margin-left: 130px;
        min-height: 36px
    }
</style>

{include file="public/footer" /}
<script>
    layui.use(['form', 'upload','element', 'jquery'], function(){
        var $ = layui.$
            ,form = layui.form
            ,element = layui.element
            ,upload = layui.upload;
        getData(form,'base');//获取基本设置数据
        uploadimg(upload,'upload1','uploadLogo','base')//基本设置上传LOGO
        uploadimg(upload,'upload2','mp_verify','wx');//上传核验文件
        uploadimg(upload,'upload3','mch_key','wx');//商户key
        uploadimg(upload,'upload4','mch_cert','wx');//商户cert

        element.on('tab(settab)', function(elem){
            if(elem.index==1){
                getData(form,'wx')
            }
        });
        //监听状态操作
        form.on('submit(savedata1)', function(data){
            data.field.logo = $("#uploadLogo").find('img').attr('src');
            ajaxSavedata(data.field,'base')
            return false;
        });

        //监听提交
        form.on('submit(savedata2)', function(data){

            data.field.verifyfile = $("#verifyfile").find('img').attr('src');
            data.field.mch_key = $("#mch_key").find('img').attr('src');
            data.field.mch_cert = $("#mch_cert").find('img').attr('src');

            ajaxSavedata(data.field,'wx')
            return false;
        });
        form.render();
    });

    function getData(form,name) {
        var loadconfigurl = "{:url('sysconfig/loadconfig')}" + "?name="+name;

        $.ajax({
            type: "get",
            url: loadconfigurl,
            success: function (response) {
                if(response!=null&&response!=""){

                    if(name=='base'){
                        if(response.logo){
                            $('#uploadLogo').removeClass();
                            $('#uploadLogo').find('img').attr('src', response.logo);
                        }

                        form.val('baseset', {
                            "title": response.title
                            ,"copyright": response.copyright
                            ,"copyrighturl": response.copyrighturl
                        });
                    }else {
                        $('#mp_verify').removeClass();
                        $('#mch_key').removeClass();
                        $('#mch_cert').removeClass();

                        $('#mp_verify').find('img').attr('src', response.mp_verify);
                        $('#mch_key').find('img').attr('src', response.mch_key);
                        $('#mch_cert').find('img').attr('src', response.mch_cert);
                        form.val('wxset', {
                            "salt": response.salt
                            ,"tablename": response.tablename
                            ,"domain": response.domain
                            ,"appid": response.appid
                            ,"appsecret": response.appsecret
                            ,"mch_id": response.mch_id
                            ,"key": response.key
                            ,"notifyurl": response.notifyurl
                            ,"returnurl": response.returnurl
                            ,"mp_verify": response.mp_verify
                            ,"mch_key": response.mch_key
                            ,"mch_cert": response.mch_cert
                        });
                    }

                }

            }
        });
    }
    function uploadimg(upload,elemid,uploadid,name) {
        var uploadimgurl = "{:url('/comm/upload/uploadimg')}" + "?name="+name;
        upload.render({
            elem: '#'+elemid
            ,url: uploadimgurl //改成您自己的上传接口
            ,done: function(res){
                layer.msg('上传成功');
                layui.$('#'+uploadid).removeClass('layui-hide').find('img').attr('src', res.data);
            }
        });
    }

    function ajaxSavedata(data,name) {

        var postdata = JSON.stringify(data);
        var saveconfigurl = "{:url('sysconfig/saveconfig')}" + "?name="+name;

        $.ajax({
            type: "POST",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            url: saveconfigurl,
            data: postdata,
            success: function (response) {
                layer.msg('保存成功');
            }
        });

    }

</script>
</body>
</html>
